package com.Practice.estore1;
import java.util.Comparator;
import java.util.List;
import java.util.Scanner;
public class ConsoleShop {
    private final ProductService productService;
    private final Scanner scanner;

    public ConsoleShop() {
        productService = new ProductService();
        scanner = new Scanner(System.in);
    }

    public void start() {
        while (true) {
            showMenu();
            int choice = scanner.nextInt();
            scanner.nextLine(); // 消耗换行符

            switch (choice) {
                case 1:
                    browseAllProducts();
                    break;
                case 2:
                    browseSortedProducts();
                    break;
                case 0:
                    System.out.println("感谢使用商城系统！");
                    return;
                default:
                    System.out.println("无效选择，请重新输入！");
            }
        }
    }

    private void showMenu() {
        System.out.println("\n=== 商城系统 ===");
        System.out.println("1. 浏览所有商品");
        System.out.println("2. 排序浏览商品");
        System.out.println("0. 退出");
        System.out.print("请选择操作: ");
    }

    private void browseAllProducts() {
        System.out.println("\n=== 所有商品 ===");
        List<Product> products = productService.getAllProducts();
        for (Product product : products) {
            System.out.println(product);
        }
    }

    private void browseSortedProducts() {
        System.out.println("\n=== 排序浏览商品 ===");
        List<Product> products = productService.getAllProducts();

        // 按价格排序
        products.sort(Comparator.comparingDouble(Product::getPrice));

        System.out.println("按价格升序排列:");
        for (Product product : products) {
            System.out.println(product);
        }
    }

    public static void main(String[] args) {
        ConsoleShop shop = new ConsoleShop();
        shop.start();
    }
}